


<!DOCTYPE html>
<html id="htmlId">
<head>
  <title>Coverage Report > CartDaoImpl</title>
  <style type="text/css">
    @import "../../css/coverage.css";
    @import "../../css/highlight-idea.css";
  </style>
  <script type="text/javascript" src="../../js/highlight.pack.js"></script>
</head>

<body>
<div class="content">
<div class="breadCrumbs">
Current scope:     <a href="../../index.html">all classes</a>
    <span class="separator">|</span>
    <a href="../index.html">com.example.bookstorebackend.daoimpl</a>
</div>

<h1>Coverage Summary for Class: CartDaoImpl (com.example.bookstorebackend.daoimpl)</h1>

<table class="coverageStats">
<tr>
  <th class="name">Class</th>
<th class="coverageStat 
">
  Class, %
</th>
<th class="coverageStat 
">
  Method, %
</th>
<th class="coverageStat 
">
  Line, %
</th>
</tr>
<tr>
  <td class="name">CartDaoImpl</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (1/1)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    100%
  </span>
  <span class="absValue">
    (11/11)
  </span>
</td>
<td class="coverageStat">
  <span class="percent">
    95.4%
  </span>
  <span class="absValue">
    (62/65)
  </span>
</td>
</tr>

</table>

<br/>
<br/>


<pre>
<div class="sourceCode" id="sourceCode"><i class="no-highlight">1</i>&nbsp;package com.example.bookstorebackend.daoimpl;
<i class="no-highlight">2</i>&nbsp;
<i class="no-highlight">3</i>&nbsp;import com.example.bookstorebackend.dao.CartDao;
<i class="no-highlight">4</i>&nbsp;import com.example.bookstorebackend.dao.OrderDao;
<i class="no-highlight">5</i>&nbsp;import com.example.bookstorebackend.entity.*;
<i class="no-highlight">6</i>&nbsp;import com.example.bookstorebackend.repository.*;
<i class="no-highlight">7</i>&nbsp;import net.sf.json.JSONObject;
<i class="no-highlight">8</i>&nbsp;import org.springframework.beans.factory.annotation.Autowired;
<i class="no-highlight">9</i>&nbsp;import org.springframework.stereotype.Repository;
<i class="no-highlight">10</i>&nbsp;
<i class="no-highlight">11</i>&nbsp;import java.util.ArrayList;
<i class="no-highlight">12</i>&nbsp;import java.util.List;
<i class="no-highlight">13</i>&nbsp;import java.util.concurrent.atomic.AtomicReference;
<i class="no-highlight">14</i>&nbsp;
<i class="no-highlight">15</i>&nbsp;@Repository
<b class="fc"><i class="no-highlight">16</i>&nbsp;public class CartDaoImpl implements CartDao {</b>
<i class="no-highlight">17</i>&nbsp;    @Autowired
<i class="no-highlight">18</i>&nbsp;    private OrderRepository orderRepository;
<i class="no-highlight">19</i>&nbsp;    @Autowired
<i class="no-highlight">20</i>&nbsp;    private OrderItemRepository orderItemRepository;
<i class="no-highlight">21</i>&nbsp;    @Autowired
<i class="no-highlight">22</i>&nbsp;    private UserRepository userRepository;
<i class="no-highlight">23</i>&nbsp;    @Autowired
<i class="no-highlight">24</i>&nbsp;    private BookRepository bookRepository;
<i class="no-highlight">25</i>&nbsp;    @Autowired
<i class="no-highlight">26</i>&nbsp;    private CartRepository cartRepository;
<i class="no-highlight">27</i>&nbsp;    @Autowired
<i class="no-highlight">28</i>&nbsp;    private CartItemRepository cartItemRepository;
<i class="no-highlight">29</i>&nbsp;    @Autowired
<i class="no-highlight">30</i>&nbsp;    private OrderDao orderDao;
<i class="no-highlight">31</i>&nbsp;
<i class="no-highlight">32</i>&nbsp;    @Override
<i class="no-highlight">33</i>&nbsp;    public Cart addCart(Integer userId,Integer bookId){
<b class="fc"><i class="no-highlight">34</i>&nbsp;        User user=userRepository.getOne(userId);</b>
<b class="fc"><i class="no-highlight">35</i>&nbsp;        Book book=bookRepository.getOne(bookId);</b>
<b class="fc"><i class="no-highlight">36</i>&nbsp;        Cart cart=new Cart();</b>
<b class="fc"><i class="no-highlight">37</i>&nbsp;        cart.setUser(user);</b>
<b class="fc"><i class="no-highlight">38</i>&nbsp;        cartRepository.save(cart);</b>
<b class="fc"><i class="no-highlight">39</i>&nbsp;        CartItem cartItem=new CartItem();</b>
<b class="fc"><i class="no-highlight">40</i>&nbsp;        cartItem.setCart(cart);</b>
<b class="fc"><i class="no-highlight">41</i>&nbsp;        cartItem.setBook(book);</b>
<i class="no-highlight">42</i>&nbsp;        /*默认购买本数为1*/
<b class="fc"><i class="no-highlight">43</i>&nbsp;        Integer number=1;</b>
<b class="fc"><i class="no-highlight">44</i>&nbsp;        Integer price=book.getPrice()*number;</b>
<b class="fc"><i class="no-highlight">45</i>&nbsp;        cartItem.setItem_number(number);</b>
<b class="fc"><i class="no-highlight">46</i>&nbsp;        cartItem.setPrice(price);</b>
<b class="fc"><i class="no-highlight">47</i>&nbsp;        cartItemRepository.save(cartItem);</b>
<b class="fc"><i class="no-highlight">48</i>&nbsp;        return cart;</b>
<i class="no-highlight">49</i>&nbsp;    }
<i class="no-highlight">50</i>&nbsp;
<i class="no-highlight">51</i>&nbsp;    @Override
<i class="no-highlight">52</i>&nbsp;    public JSONObject getCart(Integer userId){
<b class="fc"><i class="no-highlight">53</i>&nbsp;        List&lt;Cart&gt; carts=cartRepository.getAllByUser_UserId(userId);</b>
<b class="fc"><i class="no-highlight">54</i>&nbsp;        List&lt;CartItem&gt; cartItems=new ArrayList&lt;&gt;();</b>
<b class="fc"><i class="no-highlight">55</i>&nbsp;        carts.forEach(cart -&gt; {</b>
<b class="fc"><i class="no-highlight">56</i>&nbsp;            cartItems.addAll(cartItemRepository.getAllByCart(cart));</b>
<i class="no-highlight">57</i>&nbsp;        });
<b class="fc"><i class="no-highlight">58</i>&nbsp;        List&lt;Book&gt; books=new ArrayList&lt;&gt;();</b>
<b class="fc"><i class="no-highlight">59</i>&nbsp;        List&lt;Integer&gt; nums=new ArrayList&lt;&gt;();</b>
<b class="fc"><i class="no-highlight">60</i>&nbsp;        List&lt;Integer&gt; prices=new ArrayList&lt;&gt;();</b>
<b class="fc"><i class="no-highlight">61</i>&nbsp;        cartItems.forEach(cartItem -&gt; {</b>
<b class="fc"><i class="no-highlight">62</i>&nbsp;            books.add(cartItem.getBook());</b>
<b class="fc"><i class="no-highlight">63</i>&nbsp;            nums.add(cartItem.getItem_number());</b>
<b class="fc"><i class="no-highlight">64</i>&nbsp;            prices.add(cartItem.getPrice());</b>
<i class="no-highlight">65</i>&nbsp;        });
<b class="fc"><i class="no-highlight">66</i>&nbsp;        JSONObject obj=new JSONObject();</b>
<b class="fc"><i class="no-highlight">67</i>&nbsp;        obj.put(&quot;books&quot;,books);</b>
<b class="fc"><i class="no-highlight">68</i>&nbsp;        obj.put(&quot;nums&quot;,nums);</b>
<b class="fc"><i class="no-highlight">69</i>&nbsp;        obj.put(&quot;prices&quot;,prices);</b>
<b class="fc"><i class="no-highlight">70</i>&nbsp;        obj.put(&quot;cart&quot;,carts);</b>
<b class="fc"><i class="no-highlight">71</i>&nbsp;        return obj;</b>
<i class="no-highlight">72</i>&nbsp;    }
<i class="no-highlight">73</i>&nbsp;
<i class="no-highlight">74</i>&nbsp;    @Override
<i class="no-highlight">75</i>&nbsp;    public JSONObject deleteOne(Integer cartId){
<b class="fc"><i class="no-highlight">76</i>&nbsp;        Cart cart=cartRepository.getOne(cartId);</b>
<b class="fc"><i class="no-highlight">77</i>&nbsp;        CartItem cartItem=cartItemRepository.getCartItemByCart(cart);</b>
<b class="fc"><i class="no-highlight">78</i>&nbsp;        cartItemRepository.delete(cartItem);</b>
<b class="fc"><i class="no-highlight">79</i>&nbsp;        cartRepository.delete(cart);</b>
<b class="fc"><i class="no-highlight">80</i>&nbsp;        JSONObject obj =new JSONObject();</b>
<b class="fc"><i class="no-highlight">81</i>&nbsp;        if(!cartRepository.findById(cartId).isPresent()){</b>
<b class="fc"><i class="no-highlight">82</i>&nbsp;            obj.put(&quot;msg&quot;,&quot;success&quot;);</b>
<i class="no-highlight">83</i>&nbsp;        }
<i class="no-highlight">84</i>&nbsp;        else {
<b class="nc"><i class="no-highlight">85</i>&nbsp;            obj.put(&quot;msg&quot;,&quot;fail&quot;);</b>
<i class="no-highlight">86</i>&nbsp;        }
<b class="fc"><i class="no-highlight">87</i>&nbsp;        return obj;</b>
<i class="no-highlight">88</i>&nbsp;    }
<i class="no-highlight">89</i>&nbsp;    @Override
<i class="no-highlight">90</i>&nbsp;    public JSONObject deleteAll(List&lt;Integer&gt; cartIds){
<b class="fc"><i class="no-highlight">91</i>&nbsp;        for (Integer cartId : cartIds) {</b>
<b class="fc"><i class="no-highlight">92</i>&nbsp;            deleteOne(cartId);</b>
<b class="fc"><i class="no-highlight">93</i>&nbsp;        }</b>
<b class="fc"><i class="no-highlight">94</i>&nbsp;        return assertCarts(cartIds);</b>
<i class="no-highlight">95</i>&nbsp;    }
<i class="no-highlight">96</i>&nbsp;
<i class="no-highlight">97</i>&nbsp;    @Override
<i class="no-highlight">98</i>&nbsp;    public Order buyOne(Integer cartId){
<b class="fc"><i class="no-highlight">99</i>&nbsp;        Cart cart=cartRepository.getOne(cartId);</b>
<b class="fc"><i class="no-highlight">100</i>&nbsp;        CartItem cartItem=cartItemRepository.getCartItemByCart(cart);</b>
<b class="fc"><i class="no-highlight">101</i>&nbsp;        Book book=cartItem.getBook();</b>
<b class="fc"><i class="no-highlight">102</i>&nbsp;        User user=cart.getUser();</b>
<b class="fc"><i class="no-highlight">103</i>&nbsp;        cartItemRepository.delete(cartItem);</b>
<b class="fc"><i class="no-highlight">104</i>&nbsp;        cartRepository.delete(cart);</b>
<b class="fc"><i class="no-highlight">105</i>&nbsp;        return orderDao.addOne(user.getUserId(),book.getBookId());</b>
<i class="no-highlight">106</i>&nbsp;    }
<i class="no-highlight">107</i>&nbsp;
<i class="no-highlight">108</i>&nbsp;    @Override
<i class="no-highlight">109</i>&nbsp;    public JSONObject buyAll(List&lt;Integer&gt; cartIds){
<b class="fc"><i class="no-highlight">110</i>&nbsp;        for (Integer cartId : cartIds) {</b>
<b class="fc"><i class="no-highlight">111</i>&nbsp;            buyOne(cartId);</b>
<b class="fc"><i class="no-highlight">112</i>&nbsp;        }</b>
<b class="fc"><i class="no-highlight">113</i>&nbsp;        return assertCarts(cartIds);</b>
<i class="no-highlight">114</i>&nbsp;    }
<i class="no-highlight">115</i>&nbsp;
<i class="no-highlight">116</i>&nbsp;    private JSONObject assertCarts(List&lt;Integer&gt; cartIds) {
<b class="fc"><i class="no-highlight">117</i>&nbsp;        JSONObject obj =new JSONObject();</b>
<b class="fc"><i class="no-highlight">118</i>&nbsp;        AtomicReference&lt;Boolean&gt; flag= new AtomicReference&lt;&gt;(true);</b>
<b class="fc"><i class="no-highlight">119</i>&nbsp;        cartIds.forEach(integer -&gt; {</b>
<b class="fc"><i class="no-highlight">120</i>&nbsp;            if(cartRepository.findById(integer).isPresent()){</b>
<b class="nc"><i class="no-highlight">121</i>&nbsp;                flag.set(false);</b>
<i class="no-highlight">122</i>&nbsp;            }
<i class="no-highlight">123</i>&nbsp;        });
<b class="fc"><i class="no-highlight">124</i>&nbsp;        if(flag.get()){</b>
<b class="fc"><i class="no-highlight">125</i>&nbsp;            obj.put(&quot;msg&quot;,&quot;success&quot;);</b>
<i class="no-highlight">126</i>&nbsp;        }
<i class="no-highlight">127</i>&nbsp;        else{
<b class="nc"><i class="no-highlight">128</i>&nbsp;            obj.put(&quot;msg&quot;,&quot;fail&quot;);</b>
<i class="no-highlight">129</i>&nbsp;        }
<b class="fc"><i class="no-highlight">130</i>&nbsp;        return obj;</b>
<i class="no-highlight">131</i>&nbsp;    }
<i class="no-highlight">132</i>&nbsp;}
</div>
</pre>
</div>

<script type="text/javascript">
(function() {
    var msie = false, msie9 = false;
    /*@cc_on
      msie = true;
      @if (@_jscript_version >= 9)
        msie9 = true;
      @end
    @*/

    if (!msie || msie && msie9) {
        var codeBlock = document.getElementById('sourceCode');

        if (codeBlock) {
            hljs.highlightBlock(codeBlock);
        }
    }
})();
</script>

<div class="footer">
    
    <div style="float:right;">generated on 2022-04-14 16:08</div>
</div>
</body>
</html>
